﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using DDNY.WebPortals.Common;


/// <summary>
/// Summary description for ForumHelper
/// </summary>
public class FeedXmlHelper
{

    private const string categoriesCacheKey = "categoriesCacheKey";

    private static List<FeedCategory> currentFeedCategories;

    public static List<FeedCategory> CurrentFeedCategories
    {
        get
        {

            if (currentFeedCategories == null)
            {
                currentFeedCategories = GetCategories();
            }

            return currentFeedCategories;
        }
    }

    public static Dictionary<int, RssFeed> FeedsDic { get; set; }

    public static RssFeed GetFeedByHashCode(int hashcode)
    {
        return FeedsDic[hashcode];
    }

    public static List<RssFeed> GetFeedsByCategory(string queryString)
    {
        var q = from n in CurrentFeedCategories
                where n.internalName == queryString
                select n;
        List<RssFeed> result = q.SingleOrDefault().Feeds;
        return result;
    }

    public static FeedCategory GetCategoryByQueryString(string queryString)
    {
        var q = from n in CurrentFeedCategories
                where n.internalName == queryString
                select n;
        FeedCategory result = q.SingleOrDefault();
        return result;
    }

    private static List<FeedCategory> GetCategories()
    {
        List<FeedCategory> result = new List<FeedCategory>();
        XmlDocument doc = new XmlDocument();
        Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
        string forumsFile = "feeds.xml";
        doc.Load(forumsFile);
        foreach (XmlNode item in doc.SelectNodes("feeds/categories/category"))
        {
            FeedCategory catToAdd = new FeedCategory();
            catToAdd.internalName = item.Attributes["querystring"].InnerText;
            catToAdd.Title = item.Attributes["categoryname"].InnerText;
            catToAdd.Feeds = ExtractFeedsFromNode(item);
            result.Add(catToAdd);
        }

        return result;
    }

    private static List<RssFeed> ExtractFeedsFromNode(XmlNode node)
    {
        List<RssFeed> result = new List<RssFeed>();
        foreach (XmlNode item in node.ChildNodes)
        {
            RssFeed feedToAdd = new RssFeed();
            feedToAdd.FeedTitle = item.Attributes["feedname"].InnerText;
            feedToAdd.FeedUrl = item.Attributes["feedurl"].InnerText;
            //feedToAdd.FeedUrl = feedToAdd.FeedUrl.Replace("&amp;", "&");
            feedToAdd.ShortTitle = item.Attributes["shortname"].InnerText;
            feedToAdd.FeedDate = DateTime.Now;
            result.Add(feedToAdd);
            if (FeedsDic == null)
            {
                FeedsDic = new Dictionary<int, RssFeed>();
            }
            if (!FeedsDic.ContainsKey(feedToAdd.FeedUrl.GetHashCode()))
            {
                FeedsDic.Add(feedToAdd.FeedUrl.GetHashCode(), feedToAdd);
            }

        }
        return result;
    }




}
